package com.link2room.cms.service.impl;

import com.link2room.cms.dto.param.base.CmsBaseParamDto;
import com.link2room.cms.dto.result.CmsBaseDto;
import com.link2room.cms.entity.ArticleBase;
import com.link2room.cms.entity.ArticleDetail;
import com.link2room.cms.entity.CmsBase;
import com.link2room.cms.entity.CmsBaseDetail;
import com.link2room.cms.service.ICmsBaseService;
import com.link2room.common.entity.BaseEntity;
import com.link2room.common.jpa.condition.ConditionDetail;
import com.link2room.common.jpa.condition.L2RCondition;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sqj88
 * @date 2017/12/1 11:18
 */
@Service("cmsBaseService")
public class CmsBaseServiceImpl implements ICmsBaseService {
    @Resource
    private ICommonService commonService;

    @Override
    @Transactional
    public void saveOrUpdateCmsBase(CmsBase cmsBase, CmsBaseDetail cmsBaseDetail)throws Exception{
        commonService.saveOrUpdate(cmsBase);
        commonService.saveOrUpdate(cmsBaseDetail);
    }

    @Override
    public List<CmsBaseDto> getCmsBaseTree(CmsBaseParamDto cmsBaseParamDto)throws Exception{
        StringBuffer sb=new StringBuffer("SELECT\n" +
                "  a.lhotel_group_code lhotelGroupCode," +
                "  a.lhotel_code lhotelCode, " +
                "  a.id, " +
                "  a.parent_code parentCode, " +
                "  a.category_code categoryCode, " +
                "  a.group_type groupType, " +
                "  a.is_halt isHalt, " +
                "  a.list_order listOrder, " +
                "  a.create_user createUser, " +
                "  a.create_datetime createDatetime, " +
                "  a.modify_user modifyUser, " +
                "  a.modify_datetime modifyDatetime, " +
                "  b.descript descript, " +
                "  b.lit_pic litPic, " +
                "b.`language`,b.`title`,b.`url`,b.`template` \n" +
                "FROM\n" +
                "  cms_base AS a\n" +
                "  LEFT JOIN cms_base_detail AS b ON  a.`lhotel_group_code`=b.`lhotel_group_code` AND  a.`category_code`=b.`category_code`\n" +
                "  AND b.language=?1   "+
                "WHERE a.lhotel_group_code = ?2 \n" +
                "  AND FIND_IN_SET(\n" +
                "    a.parent_code,\n" +
                "    get_cms_base_tree (?3) \n" +
                "  )\n" +
                "\n");
        List<Object> objectList=new ArrayList();
        objectList.add(cmsBaseParamDto.getLanguage());
        objectList.add(cmsBaseParamDto.getLhotelGroupCode());
        objectList.add(cmsBaseParamDto.getParentCode());
        int i=3;

        if(StringUtil.isNotBlank(cmsBaseParamDto.getIsHalt())){
            i++;
            sb.append(" AND a.`is_halt`=?"+i+"  ");
            objectList.add(cmsBaseParamDto.getIsHalt());
        }
        if(StringUtil.isNotBlank(cmsBaseParamDto.getGroupType())){
            i++;
            sb.append("AND  FIND_IN_SET(?"+i+" ,group_type)");
            objectList.add(cmsBaseParamDto.getGroupType());
        }
        QueryResult<CmsBaseDto> cmsBaseDtoQueryResult = commonService.findByNative(sb.toString(), null, objectList, null, CmsBaseDto.class, null, null);
        return cmsBaseDtoQueryResult.getResultlist();
    }

    @Override
    @Transactional
    public void deleteCmsBase(String lhotelGroupCode,List<String> categorys, Object[] ids) throws Exception {
        L2RCondition l2RCondition=new L2RCondition();
        l2RCondition.setAndConditions(new ConditionDetail[]{
                new ConditionDetail(BaseEntity.BaseEntity_.lhotelGroupCode.toString(), CriteriaUtil.Operator.EQ,lhotelGroupCode),
                new ConditionDetail(CmsBase.CmsBase_.categoryCode.toString(), CriteriaUtil.Operator.IN,categorys),
        });

        commonService.deleteByCondition(CmsBaseDetail.class,l2RCondition);

        QueryResult<ArticleBase> articleBaseQueryResult = commonService.findAllByCondition(ArticleBase.class, l2RCondition);
        if(articleBaseQueryResult.getResultlist()!=null){
            List<String> articleIds=new ArrayList<>();
            for(int i=0;i<articleBaseQueryResult.getResultlist().size();i++){
                articleIds.add(articleBaseQueryResult.getResultlist().get(i).getArticleId());
            }
            if(articleIds.size()>0){
                l2RCondition.setAndConditions(new ConditionDetail[]{
                        new ConditionDetail(ArticleDetail.ArticleDetail_.articleId.toString(), CriteriaUtil.Operator.IN,articleIds)});
                commonService.deleteByCondition(ArticleBase.class,l2RCondition);
                commonService.deleteByCondition(ArticleDetail.class,l2RCondition);
            }
        }
        commonService.delete(CmsBase.class,ids);
    }
}
